﻿using UnityEngine;

namespace MT_ToolKit
{
    /// <summary>
    /// 单例模式 Behaviour 基类
    /// </summary>
    public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
    {
        protected static T mInstance;

        public static T Instance
        {
            get
            {
                if (mInstance == null)
                {
                    mInstance = Object.FindObjectOfType<T>();
                }
                if (mInstance == null)
                {
                    GameObject obj = new GameObject(typeof(T).Name);
                    mInstance = obj.AddComponent<T>();
                    DontDestroyOnLoad(mInstance);
                }
                return mInstance;
            }
        }
    }

    public abstract class MonoSingletonDestroy<T> : MonoBehaviour where T : MonoBehaviour
    {
        protected static T mInstance;

        public static T Instance
        {
            get
            {
                if (mInstance == null)
                {
                    mInstance = Object.FindObjectOfType<T>();
                }
                if (mInstance == null)
                {
                    GameObject obj = new GameObject(typeof(T).Name);
                    mInstance = obj.AddComponent<T>();
                }
                return mInstance;
            }
        }

    }
}

